<?php

declare (strict_types=1);

namespace app\common\model;

use think\db\BaseQuery;
use think\model\Pivot;
/**
 * 模型基类
 * Class Base
 */
abstract class Base extends Pivot
{

    /**
     * 错误信息
     * @var string
     */
    protected string $error = '';

    /**
     * 模型基类初始化
     */
    public static function init()
    {
        parent::init();
    }

    /**
     * 查找单条记录
     * @param mixed $data 查询条件
     * @param array $with 关联查询
     * @return array|static|null
     */
    public static function get($data, array $with = []): array|static|null
    {
        try {
            $query = (new static())->with($with);
            return is_array($data) ? $query->where($data)->find() : $query->find((int)$data);
        } catch (\Exception $e) {
            return null;
        }
    }
    
    /**
     * 字段值增长
     * @param int|bool|array $where
     * @param string $field
     * @param float $step
     * @return mixed
     */
    protected function setInc(int|bool|array $where, string $field, float $step = 1): mixed
    {
        if (is_numeric($where)) {
            $where = [$this->getPk() => (int)$where];
        }
        return $this->where($where)->inc($field, $step)->update();
    }

    /**
     * 字段值消减
     * @param int|bool|array $where
     * @param string $field
     * @param float $step
     * @return mixed
     */
    protected function setDec(int|bool|array $where, string $field, float $step = 1): mixed
    {
        if (is_numeric($where)) {
            $where = [$this->getPk() => (int)$where];
        }
        return $this->where($where)->dec($field, $step)->update();
    }

    /**
     * 删除记录
     * @param array $where
     *  方式1: ['goods_id' => $goodsId]
     *  方式2: [
     *           ['user_id', '=', $userId],
     *           ['role_id', 'in', $deleteRoleIds]
     *        ]
     * @return bool|int
     */
    public static function deleteAll(array $where): bool|int
    {
        return (new static())->where($where)->delete();
    }

    /**
     * 新增hidden属性
     * @param array $hidden
     * @return $this
     */
    protected function addHidden(array $hidden): Base
    {
        $this->hidden = array_merge($this->hidden, $hidden);
        return $this;
    }

    /**
     * 获取隐藏的属性
     * @param array $hidden
     * @return array
     */
    public static function getHidden(array $hidden = []): array
    {
        $model = new static();
        return array_merge($model->hidden, $hidden);
    }

    /**
     * 实例化新查询对象
     * @return BaseQuery
     */
    protected function getNewQuery(): \think\db\BaseQuery
    {
        return $this->db();
    }

    /**
     * 设置错误信息
     * @param string $error
     * @return bool
     */
    protected function setError(string $error): bool
    {
        $this->error = $error ?: '未知错误';
        return false;
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError(): string
    {
        return $this->error;
    }

    /**
     * 是否存在错误信息
     * @return bool
     */
    public function hasError(): bool
    {
        return !empty($this->error);
    }

    /**
     * 设置默认的检索数据
     * @param array $query
     * @param array $default
     * @return array
     */
    public function setQueryDefaultValue(array $query, array $default = []): array
    {
        return setQueryDefaultValue($query, $default);
    }


}
